﻿


#include "FwbLog.h"
#include "AdvMedia.h"
#include "KccMain.h"
#include "Encoder.h"

static bool setValue(ENCODE_RAM* encr, int offset, int value, bool write) {
	encr->RamBase[encr->SetupOffset + offset] = value;
	//		MAPI.DWORD_BUFF8(value, gArray + SetupOffset + offset, WavData);     // Java的地址需要+gArray
	if (write) {
		int result = coreFileControl(encr->EffTyp, encr->Track, SETTYP_CONTROL);//			writeControl();
		if (result < 0) {
			encr->Error = true;
			return false;
		}
	}
	return true;
}
static bool setChanne(ENCODE_RAM* encr, bool enaVideo, bool enaAudio, int sampRate, int channel) {
	setValue(encr, SET_SAMP_RATE, sampRate, false);
	setValue(encr, SET_CHANNEL, channel, false);
	int value = enaAudio ? 0x00000101 : 0x00000000;         // B15-8=1初始化音频 B7-0=1允许音频
	if (enaVideo) value |= 0x01010000;                      // B31-24=1初始化视频 B23-16=1允许视频
	setValue(encr, SET_BLOCK_SIZE, AUD_EFF_BK_SIZE, false); // 初始化时必须写入，每块需要的字节长度
	setValue(encr, SET_ENABLE_AV, value, true);           // 设置B15-8=1允许视频 B7-0=1允许音频
	return !encr->Error;
}


static bool controlInit(ENCODE_RAM* encr, int sampRate, int channel) {
	KCC_WAVE_INFO* kwi = &encr->Kwi;
	kwi->SampRate = sampRate;                               // 采样率
	kwi->SampLeng = AUD_EFF_BK_SIZE;                                          // 采样长度
	kwi->SampLargest = AUD_EFF_BK_SIZE;                                       // 最大的采样长度，就是当前结构分配的长度
	kwi->ChQty = channel;
	kwi->ChFlag = 0x03;
	kwi->ChMore = 0;
	KccWaveInfoSetWave(kwi);
	setValue(encr, SET_MEDTYP, MEDTYP_OPUS, false);	// MEDTYP_AC3	MEDTYP_OPUS
	setValue(encr, SET_BITRATE, 64000, false);				// [10]	设置编码每秒字节数
	setChanne(encr, false, true, sampRate, channel);
	//coreFileSetup(encr->EffTyp, encr->Track, SETTYP_CONTROL, 0, NULL, NULL) == 0 ? true : false;
	return !encr->Error;
}

bool EncodeAudio(ENCODE_RAM* encr, AUD_EFF_DOUBLE* array[AUD_EFF_CH_SIZE]) {
	KCC_WAVE_INFO* kwi = &encr->Kwi;
	for (int channel = 0; channel < (int)kwi->ChQty; channel++) {
		for (int index = 0; index < AUD_EFF_BK_SIZE; index++) {
			kwi->Wave[channel][index] = 0;// (float)array[channel][index];
		}
		int tt = 0;
	}
	int result = coreFileAudio(encr->EffTyp, encr->Track, 0, kwi);
	return result == 0 ? true : false;
}
bool EncodeStop(ENCODE_RAM* encr) {
	coreFileControl(encr->EffTyp, encr->Track, SETTYP_CLOSE);
	return true;
}
bool EncodeStart(ENCODE_RAM* encr, int sampRate, int channel) {      // 开始运行模块
	memset(encr, 0, sizeof(ENCODE_RAM));
	encr->EffTyp = EFFECT_ENCODE;
	encr->SetupOffset = 0x10;
	int maxTrack = coreFileControl(0, 0, SETTYP_MAX);		// 获取KcCore的最大track
	for (int index = 1; index < maxTrack; index++) {
		int result = coreFileControl(encr->EffTyp, index, SETTYP_CHECK);
		//            MSTRING(String.format("PlayAJ setStart E %s(%s)%s(%s)", result, index, EffTyp, length));
		if (result >= 0) {
			encr->Track = result;
			result = coreFileSetup(encr->EffTyp, result, SETTYP_OPEN, 0x10, encr->RamBase, NULL);
			if (result >= 0) {
				return controlInit(encr, sampRate, channel);
			}
		}
	}
	return false;
}
